#ifndef RTSP_SERVICE_RTSP_UNIT_H
#define RTSP_SERVICE_RTSP_UNIT_H

#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "rtp_unit.h"
#include "TWList.h"
#include "str/PBufStr.h"
#include "BSocketFile.h"
#include "Debug.h"
#include "rtsp_service.h"
#include "rtp_user.h"



#define RTSPUNIT_RET_ERR        -1
#define RTSPUNIT_RET_SUCCEED    0
#define RTSPUNIT_RET_WAIT       1
#define RTSPUNIT_RET_CANCEL     2


#define RTSPUNIT_BUF_SIZE 1024*10
#define RTSPUNIT_TMP_BUF_SIZE 4096
#define RTSPUNIT_RECVBUF_SIZE 1024


#define RTSPINFO_URL_SIZE 2048
#define RTSPINFO_VERSION_SIZE 16
#define RTSPINFO_USER_AGENT_SIZE 64

//RTSP信息
typedef struct RTSPInfo {
    PBufStr url;
    char url_buf[RTSPINFO_URL_SIZE];                  //资源路径
    PBufStr version;
    char version_buf[RTSPINFO_VERSION_SIZE];             //版本
    unsigned int CSeq;
    PBufStr user_agent; //请求序号
    char user_agent_buf[RTSPINFO_USER_AGENT_SIZE];
    unsigned char accept_type;                      //视频描述信息格式
    unsigned char transport_protocol;               //传输协议
    unsigned char transport_way;                    //传输方式
    unsigned int session;                           //会话ID
    double start_time;                              //开始播放时间
    double end_time;                                //结束播放时间
    unsigned short srtp_port;                       //服务器RTP端口
    unsigned short srtcp_port;                      //服务器RTCP端口
    unsigned short crtp_port;                       //客户端RTP端口
    unsigned short crtcp_port;                      //客户端RTCP端口
} RTSPInfo;

//RTSP单元
typedef struct RTSPUnit {
    struct RTSPService *service;
    TWListNode *rtp_resource;                               //RTP 单元，一个RTP单元即一个RTP资源管理器广播
    TWListNode *rtp_user;                                   //RTP用户

    struct sockaddr_in c_in;                                //RTSP 客户套接字信息
    BSocketFile socketfile;
    unsigned char socket_recv_buf[RTSPUNIT_RECVBUF_SIZE];   //接收缓冲区

    PBufStr str;                                            //缓冲区控制
    unsigned char strbuf[RTSPUNIT_BUF_SIZE];                //数据处理读写缓冲区

    PBufStr tmp_str;                                        //SDP描述缓冲区控制
    char tmp_str_buf[RTSPUNIT_TMP_BUF_SIZE];                //SDP数据缓冲区


    unsigned int unit_state;                                //rtsp状态
    unsigned int response_state;                            //rtsp解析状态
    RTSPInfo info;                                          //rtsp信息
} RTSPUnit;

extern int RTSPUnit_init(RTSPUnit *unit,struct RTSPService *service, int c_socket, struct sockaddr_in *c_in);

extern int RTSPUnit_resolver_task(RTSPUnit *unit);

#endif //RTSP_SERVICE_RTSP_UNIT_H
